<?php
/**
 * Laundry Model
 * 
 * @author CRIA Groep 1 <project@markvink.nl>
 * @version 1.0
 * @package models
 */
class Laundry extends AppModel 
{
	/**
    * @var string Model name (faster in PHP 4)
    */
	var $name = 'Laundry';
	
	/**
    * @var string displays field in select box
    */
	var $displayField = 'name';
	
	/**
    * @var array Model behavior
    */	
	var $actsAs = 'ExtendAssociations'; 

	/**
    * @var array belongsTo relations
    */	
	var $belongsTo = array(
        'Country' => array(
            'className' => 'Country'
        ),
	);
	
	/**
    * @var array hasMany relations
    */	
	var $hasMany = array(
        'Measurement' => array(
            'className' => 'Measurement'
        ),
	);
	
	/**
    * @var array hasAndBelongsToMany relations
    */	
	var $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User'
        ),
	);
	
	/**
    * @var array validation rules
    */
	var $validate = array( 
		'name' => array(            
			'between' => array(               
				'rule' => array('between', 4, 25),            
				'required' => true,                
				'message' => 'Name must be between 4 and 25 characters long.'            
			)
		),	
		'phone' => array(            
			'notEmpty'
		),	
		'address' => array(            
			'notEmpty'
		),
		'zipcode' => array(            
			'notEmpty'
		),
		'city' => array(            
			'notEmpty'
		),
	);
	
	/**
	 * Function findByUserId
	 * 
	 * Get laundries by user_id
	 * @param integer $id user_id
	 * @return array
	 */
	function findByUserId($id = null) {
		$laundries = $this->LaundriesUser->find('all', array(
			'conditions' => array(
				'LaundriesUser.user_id' => $id
			)
		));
		foreach($laundries AS $nr => $relation) {
			unset($laundries[$nr]);
			$laundries[] = $relation['LaundriesUser']['laundry_id'];
		}
		return $laundries;
	}
}
